package com.navinfo.tripanalysis.common.arithmetic.convert;

import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticExtendedData;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema;
import org.apache.spark.sql.types.DataTypes;
import org.apache.spark.sql.types.Metadata;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;

import java.util.ArrayList;
import java.util.List;

/**
 * 行程扩展数据与Parquet格式的转换
 */
public class StatisticExtendedDataParquetConvert {
    /**
     * 创建Schema
     */
    public static StructType createSchema() {
        List<StructField> list = new ArrayList<>();
        /*--start_schema*/
        list.add(new StructField("uid", DataTypes.LongType, true, Metadata.empty()));
        list.add(new StructField("tid", DataTypes.LongType, true, Metadata.empty()));
        list.add(new StructField("trip_id", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("route_start_time", DataTypes.LongType, true, Metadata.empty()));
        list.add(new StructField("route_end_time", DataTypes.LongType, true, Metadata.empty()));
        list.add(new StructField("rpm_range500_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range600_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range700_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range800_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range900_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1000_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1100_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1200_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1300_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1400_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1500_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1600_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1700_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1800_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range1900_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2000_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2100_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2200_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2300_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2400_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2500_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2600_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2700_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2800_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range2900_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range3000_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("rpm_range_over3000_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range01_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range02_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range03_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range04_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range05_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range06_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range07_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range08_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range09_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range10_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range11_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("speed_range12_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("exceed_speed_range_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque0_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque10_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque20_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque30_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque40_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque50_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque60_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque70_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque80_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque90_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque99_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("torque100_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent0_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent10_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent20_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent30_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent40_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent50_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent60_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent70_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent80_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent90_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent99_urea", DataTypes.IntegerType, true, Metadata.empty()));
        list.add(new StructField("gas_open_percent100_urea", DataTypes.IntegerType, true, Metadata.empty()));

        /*--end_schema*/

        return new StructType(list.toArray(new StructField[list.size()]));
    }

    /**
     * 创建一行数据
     */
    public static Row createRow(OuterStatisticData m, StructType schema) {
        List<Object> list = new ArrayList<>();
        /*--start_row*/
        list.add(m.getUid());
        list.add(m.getTid());
        list.add(m.getTripId());
        list.add(m.getRouteStartTime());
        list.add(m.getRouteEndTime());

        OuterStatisticExtendedData e = m.getExtendedData();
        list.add(e.getRpmRange500Urea());
        list.add(e.getRpmRange600Urea());
        list.add(e.getRpmRange700Urea());
        list.add(e.getRpmRange800Urea());
        list.add(e.getRpmRange900Urea());
        list.add(e.getRpmRange1000Urea());
        list.add(e.getRpmRange1100Urea());
        list.add(e.getRpmRange1200Urea());
        list.add(e.getRpmRange1300Urea());
        list.add(e.getRpmRange1400Urea());
        list.add(e.getRpmRange1500Urea());
        list.add(e.getRpmRange1600Urea());
        list.add(e.getRpmRange1700Urea());
        list.add(e.getRpmRange1800Urea());
        list.add(e.getRpmRange1900Urea());
        list.add(e.getRpmRange2000Urea());
        list.add(e.getRpmRange2100Urea());
        list.add(e.getRpmRange2200Urea());
        list.add(e.getRpmRange2300Urea());
        list.add(e.getRpmRange2400Urea());
        list.add(e.getRpmRange2500Urea());
        list.add(e.getRpmRange2600Urea());
        list.add(e.getRpmRange2700Urea());
        list.add(e.getRpmRange2800Urea());
        list.add(e.getRpmRange2900Urea());
        list.add(e.getRpmRange3000Urea());
        list.add(e.getRpmRangeOver3000Urea());
        list.add(e.getSpeedRange01Urea());
        list.add(e.getSpeedRange02Urea());
        list.add(e.getSpeedRange03Urea());
        list.add(e.getSpeedRange04Urea());
        list.add(e.getSpeedRange05Urea());
        list.add(e.getSpeedRange06Urea());
        list.add(e.getSpeedRange07Urea());
        list.add(e.getSpeedRange08Urea());
        list.add(e.getSpeedRange09Urea());
        list.add(e.getSpeedRange10Urea());
        list.add(e.getSpeedRange11Urea());
        list.add(e.getSpeedRange12Urea());
        list.add(e.getExceedSpeedRangeUrea());
        list.add(e.getTorque0Urea());
        list.add(e.getTorque10Urea());
        list.add(e.getTorque20Urea());
        list.add(e.getTorque30Urea());
        list.add(e.getTorque40Urea());
        list.add(e.getTorque50Urea());
        list.add(e.getTorque60Urea());
        list.add(e.getTorque70Urea());
        list.add(e.getTorque80Urea());
        list.add(e.getTorque90Urea());
        list.add(e.getTorque99Urea());
        list.add(e.getTorque100Urea());
        list.add(e.getGasOpenPercent0Urea());
        list.add(e.getGasOpenPercent10Urea());
        list.add(e.getGasOpenPercent20Urea());
        list.add(e.getGasOpenPercent30Urea());
        list.add(e.getGasOpenPercent40Urea());
        list.add(e.getGasOpenPercent50Urea());
        list.add(e.getGasOpenPercent60Urea());
        list.add(e.getGasOpenPercent70Urea());
        list.add(e.getGasOpenPercent80Urea());
        list.add(e.getGasOpenPercent90Urea());
        list.add(e.getGasOpenPercent99Urea());
        list.add(e.getGasOpenPercent100Urea());

        /*--end_row*/

        return new GenericRowWithSchema(list.toArray(), schema);
    }
}
